﻿var ClassX={
	create:function(){
		return function(){this.initialize.apply(this,arguments);}
	}
}
var Selector=ClassX.create();
Selector.prototype={
	initialize:function(options){
		this._containerId=options.containerId;
		this._container=$(this._containerId);
		this._items=options.items;
		this._showMode=options.showMode;
		this._val=options.defVal;
		this._txt=''||options.defTxt;
		this.onSelected=options.onSelected||function(){};
		this._colWidth=options.colWidth||60;
		this._colNum=options.colNum||2;
		this._tbxWidth=options.tbxWidth||40;
		this._conWidth=(this._colWidth+10)*this._colNum+10;
		this._colMode='float';
		this._maxSeleNum=options.maxSeleNum||0;
		this._curSeleNum=this._showMode=="multi"?this._val.length:0;
		//新增加
		this._hasPage=this.hasPage;
		this._pageSize=this.pageSize||40;
		//this.currentPageIndex=this.currentPageIndex||0;
		
		//新增加 end
		
		this.render(true);
		this.setDefault=this.setValue;
	},
	render:function(isInit){
		var container=this._container;
		var obj=this;
		var showTxt='';
		container.empty();
		if(isInit){
			container.addClass('SelectWrapSingle');
		}
		if(this._showMode=='single'){
			showTxt=this.findTxtByVal(this._val);
		}else{
			if(typeof(this._val)=='undefined'||this._val.length==0){
				showTxt='请选择';
			}else{
				for(i=0;i<this._val.length;i++){
					showTxt+=this.findTxtByVal(this._val[i])+',';
				}
			}
		}
		
		var _h6=document.createElement('h6');
		var _h5=document.createElement('h5');
		$(_h6).html(showTxt).width(this._tbxWidth).appendTo(container);
		$(_h5).appendTo(container);
		if(this._items==''){
			container.mouseover(null);
			container.mouseout(null);
			return;
		}
		
		var _ol=document.createElement('ol');
		_ol.style.display="none";
		$(_ol).appendTo(container).width(this._conWidth);
		
		
		var _ulList=[];
		var _ulT=document.createElement('ul');
		_ulT.style.padding="2px;"
		var cIp={};
		//判断是否有分页，添加分页工具条
		if(this._hasPage){//分页为True且items数大于每页显示数，
			var length=parseInt(this._items.length/this._pageSize)+(this._items.length%this._pageSize?1:0);
			if(length>1){
				for(var x=0;x<length;x++){
					var _t=document.createElement('ul');
					_t.id=this._containerId.substr(1)+'_'+x;
					_t.style.height=(22*(obj.pageSize/obj._colNum+(obj.pageSize%obj._colNum?1:0)))+'px';
					if(x!=this.currentPageIndex){//非当前页面，不显示
						_t.style.display='none';						
					}
					
					_ulList.push(_t);
					$(_t).appendTo($(_ol));
				}
				
				//添加分页工具条
				var t=document.createElement('table');
				$(t).appendTo($(_ol));
				t.className='x-selector-page';
				var tr=document.createElement('tr');
				tr.style.height='20px';
				$(tr).appendTo($(t));
				var t1=document.createElement('td');
				t1.style.borderWidth='0px';
				$(t1).appendTo($(tr));
				
				
				var first=document.createElement('input');
				first.name='btn_first';
				first.type='button';
				first.className=this.currentPageIndex!=0?'x-selector-page-first':'x-selector-page-first-disable';
				first.title='第一页';
				var firstTd=document.createElement('td');
				firstTd.style.width='20px';
				firstTd.style.borderWidth='0px';
				firstTd.style.textAlign='center';
				$(first).appendTo($(firstTd));
				$(firstTd).appendTo($(tr));
				
				
				var prev=document.createElement('input');
				prev.name='btn_first';
				prev.type='button';
				prev.className=this.currentPageIndex!=0?'x-selector-page-prev':'x-selector-page-prev-disable';
				prev.title='上一页';
				var prevTd=document.createElement('td');
				prevTd.style.width='20px';
				prevTd.style.borderWidth='0px';
				prevTd.style.textAlign='center';
				$(prev).appendTo($(prevTd));
				$(prevTd).appendTo($(tr));
				
				var mTd=document.createElement('td');
				mTd.style.width='50px';
				mTd.style.borderWidth='0px';
				mTd.style.textAlign='center';
				// mTd.innerHTML=(obj.currentPageIndex+1)+'/'+length;	
				mTd.innerHTML='/'+length;			
				$(mTd).appendTo($(tr));
				
				cIp=document.createElement('input');
				cIp.className="x-selector-pageindex-input"
				cIp.value=obj.currentPageIndex+1;
				$(cIp).prependTo($(mTd));
				
				var next=document.createElement('input');
				next.name='btn_first';
				next.type='button';
				next.className=this.currentPageIndex!=length-1?'x-selector-page-next':'x-selector-page-next-disable';
				next.title='下一页';
				var nextTd=document.createElement('td');
				nextTd.style.width='20px';
				nextTd.style.borderWidth='0px';
				nextTd.style.textAlign='center';
				$(next).appendTo($(nextTd));
				$(nextTd).appendTo($(tr));
				
				
				
				var last=document.createElement('input');
				last.name='btn_first';
				last.type='button';
				last.className=this.currentPageIndex!=length-1?'x-selector-page-last':'x-selector-page-last-disable';
				last.title='最后一页';
				var lastTd=document.createElement('td');
				lastTd.style.width='20px';
				lastTd.style.borderWidth='0px';
				lastTd.style.textAlign='center';
				$(last).appendTo($(lastTd));
				$(lastTd).appendTo($(tr));
				
				
				var t2=document.createElement('td');
				t2.style.borderWidth='0px';
				$(t2).appendTo($(tr));
				
				$(first).bind('click',function(){
					if(this.className=='x-selector-page-first-disable'){
						return;
					}
					var _p=obj.currentPageIndex;
					obj.currentPageIndex=0;
					paging(_p);
				});
				
				
				$(prev).bind('click',function(){
					if(this.className=='x-selector-page-prev-disable'){
						return;
					}
					var _p=obj.currentPageIndex;
					obj.currentPageIndex-=1;
					paging(_p);
				});
				
				$(next).bind('click',function(){
					if(this.className=='x-selector-page-next-disable'){
						return;
					}
					var _p=obj.currentPageIndex;
					obj.currentPageIndex+=1;
					paging(_p);
				});
				
				$(last).bind('click',function(){
					if(this.className=='x-selector-page-last-disable'){
						return;
					}
					var _p=obj.currentPageIndex;
					obj.currentPageIndex=length-1;
					paging(_p);
				});
				
				$(cIp).bind('keydown',function(e){
					// alert(e.keyCode);
					if(e.keyCode==13||(e.keyCode>45&&e.keyCode<58)||e.keyCode==8||(e.keyCode>95&&e.keyCode<106)){
						if(e.keyCode==13){
							if(!this.value){
								return;
							}
							var _c=parseInt(this.value)
							if(_c>length||_c<1){
								return;
							}else{		
								var _p=obj.currentPageIndex;						
								obj.currentPageIndex=_c-1;
								paging(_p);
							}
						}
					}else{
						return false;
					}
				});
				
				var paging=function(oldPageIndex){
					$(obj._containerId+'_search').hide();
					cIp.value=(obj.currentPageIndex+1);
					
					$(_ulList[obj.currentPageIndex]).show();
					if(oldPageIndex!=obj.currentPageIndex){
						$(_ulList[oldPageIndex]).hide();
					}

					switch(obj.currentPageIndex){
						case 0:
							first.className='x-selector-page-first-disable';
							prev.className='x-selector-page-prev-disable';
							next.className='x-selector-page-next';
							last.className='x-selector-page-last';
							break;
						case length-1:
							first.className='x-selector-page-first';
							prev.className='x-selector-page-prev';
							next.className='x-selector-page-next-disable';
							last.className='x-selector-page-last-disable';							
							break;
						default:
							first.className='x-selector-page-first';
							prev.className='x-selector-page-prev';
							next.className='x-selector-page-next';
							last.className='x-selector-page-last';
							break;
					}
				};
				
			}else{
				$(_ulT).appendTo($(_ol));
			}
		}else{
			$(_ulT).appendTo($(_ol));
		}
				
		//判断是否有快速查询
		if(this.hasSearch&&this._items.length>1){
			var _div=document.createElement('div');
			_div.className='x-selector-search-div';
			_div.style.borderBottom="solid 1px #aaa";
			$(_div).append('<span>快速搜索：</span>');
			var _si=document.createElement('input');
			_si.className='x-selector-search-ipt';
			
			var _btni=document.createElement('input');
			_btni.type="button";
			_btni.className='x-selector-search-btn';
			var _cspan=document.createElement('span');
			_cspan.innerHTML='显示当前页';
			_cspan.style.display='inline-table';
			_cspan.style.margin='0px 0px 0px 10px';
			$(_si).appendTo($(_div));
			$(_btni).appendTo($(_div));
			$(_cspan).appendTo($(_div));
			$(_cspan).bind('click',function(){	
				_si.value='';
				displayCurrent();
			});
			
			if(this.showMode=='multi'){				
				var _selAll=document.createElement('input');
				_selAll.title='全选';
				_selAll.type='checkbox';
				$(_selAll).appendTo($(_div));
				$(_selAll).after('全选');
				
				$(_selAll).bind('click',function(){
					$(_ol).find('ul:visible').find('li').click();
				});
			}
			
			$(_ol).prepend($(_div));
			var _sul=document.createElement('ul');
			_sul.id=this._containerId.substr(1)+'_search';		
			_sul.style.display='none';
			$(_div).after($(_sul));
			
			$(_si).bind('keydown',function(e){
				if(e.keyCode==13){
					search();
				}
			});
			$(_btni).bind('click',function(e){
				search();
			});
			var displayCurrent=function(){
				cIp.value=obj.currentPageIndex+1;
				if(_ulList.length){
					$(_ulList[obj.currentPageIndex]).show();
				}else{
					$(_ulT).show();
				}
				$(_sul).hide();
			};
			var search=function(){
				if(!_si.value){					
					displayCurrent();
					return;
				}
				if(_ulList.length){
					$(_ulList[obj.currentPageIndex]).hide();
				}else{
					$(_ulT).hide();
				}
				$(_sul).html('');
				
				renderItems(_sul,_si.value);
				
				$(_sul).show();
			
			}
		}
		var renderItems=function(ul,search){
			var i,j;	
			var _count=0;
			for(i=0;i<obj._items.length;i++){
				var isCreate=true;
				if(search){	
					if(_count==obj.pageSize-1){
						break;
					}
					isCreate=obj._items[i].txt.indexOf(search)!=-1;
				}
				if(isCreate){
					_count++;
					_li=document.createElement('li');
					
					$(_li).bind('mouseenter',function(e){
						this.className='current';
					});
					
					$(_li).bind('mouseout',function(e){
						this.className='';
					});
					
					_li.p=i;
					$(_li).width(obj._colWidth);
					
					if(obj._showMode=='multi'){
						if(typeof(obj._val)=='undefined'||obj._val.length==0){
							$(_li).html("<div>"+obj._items[i].txt+"</div>");
						}else{
							for(j=0;j<obj._val.length;j++){
								if(obj._val[j]==obj._items[i].val){
									$(_li).html("<div class=\"cbx_c\" val="+obj._items[i].val+">"+obj._items[i].txt+"</div>");
									break;
								}
								
								$(_li).html("<div val=\""+obj._items[i].val+"\">"+obj._items[i].txt+"</div>");
							}
						}
					}else{
						if(obj._items[i].icon){
							$(_li).html('<span class="'+obj._items[i].icon+'"></span>'+obj._items[i].txt);
						}else{					
							$(_li).html(obj._items[i].txt);
						}
					
						//$(_li).html(obj._items[i].txt);
					}
					
					$(_li).bind('click',function(){
						container.select(this);
					});
					_li.title=obj._items[i].txt;
					if(ul){
						$(_li).appendTo($(ul));
					}else{
					
						if(_ulList.length){
							$(_li).appendTo($(_ulList[parseInt(i/obj._pageSize)]));
						}else{
							$(_li).appendTo($(_ulT));
						}
					}
				}
			}
			if(search&&!_count){
				if(ul){
					$(ul).append('<div style="text-align:center;margin:10px 0px;">无匹配项</div>');
				}
			}
		}
		
		renderItems();
		
		var intervalShow=null;
		var intervalHide=null;
		var iShow=200;
		var iHide=200;



		
		container.bind('mouseenter',function(e){
			
			if(obj.disable){//兼容disable
				return;
			}
		
			if(intervalHide!=null){
				clearTimeout(intervalHide);
			}
			
			intervalShow=setTimeout(function(){
				if(obj._hasPage){
					cIp.value=(obj.currentPageIndex+1);
				}
				
				container.children().get(2).style.display='';
				container.children().get(1).className='current';container.children().get(0).className='current';

			
			},iShow);
		});
		container.bind('mouseleave',function(e){
			
			if(obj.disable){//兼容disable
				return;
			}

			if(intervalShow!=null){
				clearTimeout(intervalShow);
			}
			
			intervalHide=setTimeout(function(){
				container.children().get(2).style.display='none';
				container.children().get(1).className='';
				container.children().get(0).className='';
			},iHide);
		});
		
		container.select=function(obj_li){
			if(obj.disable){//兼容disable
				container.children().get(2).style.display='none';
				return;
			}
			pos=obj_li.p;
			if(obj._showMode=="single"){
				container.children().get(2).style.display='none';
				container.children().get(1).className='';
				container.children().get(0).className='';
				container.children().get(0).innerHTML=obj._items[pos].txt;
				obj._txt=obj._items[pos].txt;
				obj._val=obj._items[pos].val;
				obj.onSelected();
			}else{
				if(obj._maxSeleNum==0||obj._curSeleNum<obj._maxSeleNum||$(obj_li).children().hasClass('cbx_c')){
					$(obj_li).children().toggleClass('cbx_c');
					var t_txt="";
					var t_val=new Array();
					var curSeleNum=0;
					
					$(obj._containerId+' .cbx_c').each(function(){
						t_txt+=$(this).html()+',';
						t_val.push($(this).attr('val'));
						curSeleNum++;
					});
					obj._curSeleNum=curSeleNum;
					container.children().get(0).innerHTML=t_txt==""?"请选择":t_txt;
					obj._txt=t_txt;
					obj._val=t_val;
				}else{
					alert('最多只能选择'+obj._maxSeleNum+'项');
				}
			}
		};
	},
	getValue:function(){
		return this._val;
	},
	refresh:function(data,defTxt,defVal){
		this._txt=defTxt;
		this._val=defVal;
		this._items=data;
		this.render(false);
	},
	setValue:function(){
		argsNum=arguments.length;
		if(argsNum==2){
			this._txt=arguments[0];
			this._val=arguments[1];
		}else if(argsNum==1){
			this._val=arguments[0];
			this._txt=this.findTxtByVal(this._val);
		}
		this.render(false);
	},
	findTxtByVal:function(val){
		var txt='';
		var i;
		for(i=0;i<this._items.length;i++){
			if(this._items[i].val==val){
				txt=this._items[i].txt;break;
			}
		}
		return txt;
	},
	getValueJSON:function(){
		return{txt:this._txt,val:this._val};
	},
	fireSelect:function(){
		this.onSelected.call(this);
	}
}